/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.jprvyd.gui.model;

import cz.muni.fi.pb138.jprvyd.Item;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Jan
 */
public class Item_TableModel extends AbstractTableModel {

    private static int COLUMN_COUNT = 4;
    private List<Item> items;


    public Item_TableModel(List<Item> items) {
        this.items = new ArrayList<Item>();
        for (Item item : items) {
            this.items.add(item);
        }
    }
    
    @Override
    public int getRowCount() {
        return items.size();
    }

    @Override
    public int getColumnCount() {
        return COLUMN_COUNT;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Item item = items.get(rowIndex);
        switch (columnIndex) {
            case 0:
                return item.getName();
            case 1:
                return item.getQuantity();
            case 2:
                return item.getUnit();
            case 3:
                return item.getUnitPrice();
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    public void addItem(Item item) {
        items.add(item);
        int lastRow = getRowCount() - 1;
        fireTableRowsInserted(lastRow, lastRow);
    }

    public void editItem(Item item) {
        int index = items.indexOf(item);
        items.set(index, item);
        fireTableRowsUpdated(index, index);
    }

    public void deleteItem(int row) {
        items.remove(items.get(row));
        fireTableRowsDeleted(row, row);
    }

    @Override
    public String getColumnName(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return "Name";
            case 1:
                return "Quantity";
            case 2:
                return "Unit";
            case 3:
                return "Unit Price";
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return String.class;
            case 1:
                return Integer.class;
            case 2:
                return String.class;
            case 3:
                return Integer.class;
            default:
                throw new IllegalArgumentException("columnIndex");
        }
    }
}
